package com.szholly.pro.watergas_server.ps.android.service;

import java.util.ArrayList;
import java.util.List;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.ps.android.bean.QcTaskParam;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.pro.watergas_server.ps.android.controller.QcService;
import com.szholly.pro.watergas_server.ps.data.QcWaterThanRow;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

import net.sf.json.JSONObject;

public class QcTaskBlindSpotStrategy extends QcTaskStrategy {
	public static final String QC_WATER_THAN_THANTYPE_BLIND = "1";
	public static final String QC_WATER_THAN_THANTYPE_SPOT = "2";
	QcService mDomainManager = QcService.get();

	public QcTaskBlindSpotStrategy(QcOperationService mOperationService) {
		super(mOperationService);
	}

	@Override
	public List<RowBase> getTaskList(QcTaskParam taskParam) throws Exception {
		return mOperationService.getOperationTaskList(taskParam);
	}

	@Override
	public RowBase addTask(QcTaskParam taskParam) throws Exception {
		return mOperationService.addOperationTask(taskParam);
	}

	@Override
	public void deleteTask(QcTaskParam taskParam) throws Exception {
		mOperationService.deleteOperationTask(taskParam);
	}

	@Override
	public void deleteProcessedTask(QcTaskParam taskParam) throws Exception {
		throw new Exception("自动推送任务");
	}
	
	@Override
	public RowBase getTaskInitData(QcTaskParam taskParam) throws Exception {
		return getOperationTaskInitRow(taskParam);
	}

	@Override
	public RowBase getProcessedTaskInitData(QcTaskParam taskParam) throws Exception {
		return getOperationTaskInitRow(taskParam);
	}

	@Override
	public void saveTask(QcTaskParam taskParam) throws Exception {
		updateValue(taskParam);
	}

	@Override
	public void updateTask(QcTaskParam taskParam) throws Exception {
		updateValue(taskParam);
	}

	private void updateValue(QcTaskParam taskParam) throws Exception {
		String companyCode = taskParam.getCompanyCode();
		String taskCode = taskParam.getTaskCode();
		String data = taskParam.getData();
		ValidUtils.hasText(companyCode, "公司编号为空");
		ValidUtils.hasText(taskCode, "运维任务编号不能为空");
		ValidUtils.hasText(data, "运维任务编号不能为空");

		JSONObject dataJson = JSONObject.fromObject(data);
		JSONObject valueJson = dataJson.optJSONObject("value");
		ValidUtils.notNull(valueJson, "数据为空");
		String value = valueJson.optString(QcWaterThanRow.FIELD_Value);
		String thanDate = valueJson.optString(QcWaterThanRow.FIELD_Thandate);
		String userId = valueJson.optString(QcWaterThanRow.FIELD_Userid);

		ValidUtils.hasText(value, "数值为空");
		ValidUtils.hasText(thanDate, "时间日期为空");
		ValidUtils.hasText(userId, "用户编号为空");

		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("taskCode", taskCode));
		parms.add(new GlobalParameter("companyCode", companyCode));

		parms.add(new GlobalParameter("value", value));
		parms.add(new GlobalParameter("thanDate", thanDate));
		parms.add(new GlobalParameter("userId", userId));
		String sql = "update Qc_Water_Than t\n"
				+ "   set t.value    = @value,\n"
				+ "       t.USERID    = @userId,\n"
				+ "       t.thandate = to_date(@thanDate, 'yyyy-mm-dd hh24:mi:ss')\n"
				+ " where t.companyid = @companyCode\n"
				+ "   and t.operation_task_code = @taskCode";
		int i = DataUtil.ExecuteUpdateSql(sql, parms);
		ValidUtils.isTrue(i == 0, "SQL执行异常");
	}

	private RowBase getOperationTaskInitRow(QcTaskParam taskParam) throws Exception {
		String companyCode = taskParam.getCompanyCode();
		String taskCode = taskParam.getTaskCode();

		ValidUtils.hasText(companyCode, "点位编号为空");
		ValidUtils.hasText(taskCode, "任务编号为空");

		List<GlobalParameter> parms = new ArrayList<>();
		parms.add(new GlobalParameter("companyCode", companyCode));
		parms.add(new GlobalParameter("taskCode", taskCode));
		String sql = "select t.sampleid SAMPLE_CODE,\n"
				+ "       to_char(t.thandate, 'yyyy-mm-dd hh24:mi:ss') DATE_TIME,\n"
				+ "       t.value VALUE,\n"
				+ "       t2.monitorparamname PARAMETER_NAME,\n"
				+ "       t.thantype,\n"
				+ "       t.scope,\n"
				+ "       t.ATTENUATIONWAY,\n"
				+ "       t.SOLVENT,\n"
				+ "       t.UNSURENESSLIMIT,\n"
				+ "       t.unit\n"
				+ "  from QC_WATER_THAN t\n"
				+ "  left join (select t.monitorparamname,\n"
				+ "                    max(t.monitorparamcode) monitorparamcode\n"
				+ "               from qc_monitorparam t\n"
				+ "              group by t.monitorparamname) t2\n"
				+ "    on t2.monitorparamcode = t.parmname\n"
				+ " where 1 = 1"
				+ "   and t.operation_task_code = @taskCode\n"
				+ "   and t.companyid = @companyCode";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		return rows.get(0);
	}

	
}
